package com.example.dishfo.myapplication.activity.content;

import android.net.Uri;
import android.os.Bundle;
import android.support.design.widget.BottomNavigationView;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentTransaction;
import android.view.View;
import android.widget.FrameLayout;

import com.example.dishfo.myapplication.R;
import com.example.dishfo.myapplication.activity.BaseActivity;
import com.example.dishfo.myapplication.customview.toolbar.TextTitle;
import com.example.dishfo.myapplication.util.ActivityUtil;

import butterknife.BindView;

public class ContentActivity extends BaseActivity implements
        HomeFragment.OnFragmentInteractionListener,RecommendFragment.OnFragmentInteractionListener{

    @BindView(R.id.activity_content_container)
    FrameLayout frameLayout;

    private Fragment[] fragments = new Fragment[]{null, null, null, null};
    private int currentpage = -1;

    private BottomNavigationView.OnNavigationItemSelectedListener mOnNavigationItemSelectedListener
            = item -> {
                switch (item.getItemId()) {
                    case R.id.navigation_home:
                        showFragMent(0);
                        return true;
                    case R.id.navigation_dashboard:
                        showFragMent(1);
                        return true;
                    case R.id.navigation_notifications:

                        return true;
                    case R.id.navigation_mine:

                        return true;

                }
                return false;
            };

    private View.OnClickListener listenerImage = v -> {

    };

    private void showFragMent(int index){
        FragmentManager manager=getSupportFragmentManager();
        FragmentTransaction ft=manager.beginTransaction();
        if(currentpage>=0){
            ft.hide(fragments[currentpage]);
        }

        if(fragments[index]==null){
            initFragMent(index);
            ft.add(R.id.activity_content_container,fragments[index]);
        }else {
            if(!fragments[index].isHidden())
                return;
            ft.show(fragments[index]);
        }
        ft.setCustomAnimations(R.anim.activity_enter,R.anim.activity_exit);
        ft.commit();
        currentpage=index;
    }

    private void initFragMent(int index) {
        switch (index) {
            case 0:
                fragments[index] = HomeFragment.newInstance("home", "1");
                break;
            case 1:
                fragments[index] = RecommendFragment.newInstance("recommend", "1");
                break;
            case 2:
                break;
            case 3:
                break;
        }
    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_content);
        ActivityUtil.setTransportStatusBar(getWindow());
        BottomNavigationView navigation = (BottomNavigationView) findViewById(R.id.navigation);
        TextTitle textTitle = findViewById(R.id.textTitle_activitycontent);
        textTitle.setOnClickListener(listenerImage);
        navigation.setOnNavigationItemSelectedListener(mOnNavigationItemSelectedListener);
        ActivityUtil.disableShiftMode(navigation);
        showFragMent(0);
    }


    @Override
    public void onFragmentInteraction(HomeFragment.ActionArg arg) {

    }

    @Override
    public void onFragmentInteraction(Uri uri) {

    }
}
